import os
import sys
import cProfile
import hashlib
from copy import copy

from common import *
from Board import *
from RandomAgent import *
from AIAgentMinimax import *
from AIAgentOneLookAhead import *

def playSeries(b, a1, a2, n):
    a1WinN = 0
    a2WinN = 0
    for i in range(n):
        b.clear()
        win = playGame(b, a1, a2)
        a1Win = win[0] == a1.id
        a2Win = win[0] == a2.id
        if a1Win:
            a1WinN = a1WinN + 1
        if a2Win:
            a2WinN = a2WinN + 1
        print i, float(a1WinN) / (i+1), float(a2WinN) / (i+1)
        #print win, a1Win, a2Win

def playGame(b, a1, a2):
    for i in range(100):
        a1.play(b)
        win = b.checkWin()
        if win[0] != 0:
            #b.printMe()
            break
        a2.play(b)
        win = b.checkWin()
        if win[0] != 0:
            #b.printMe()
            break
    return win
    
b = Board(5)
a1 = AIAgentOneLookAhead(1)
#a1 = AIAgentMinimax(1, 0)
a2 = AIAgentMinimax(2, 2)

playSeries(b, a1, a2, 50)
sys.exit()

playGame(b, a1, a2)
#a2.learnFromLatestGame()
a2.printHistory()
b.printMe()
print b.checkWin()
sys.exit()

for i in range(10):
    b.clear()
    print playGame(b, a1, a2)

a2.printHistory()

